package com.bj58.ecdata.dashboard.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.ServletRequestUtils;

import com.bj58.ecdata.dashboard.constant.EnumCate;
import com.bj58.ecdata.dashboard.constant.InfoConst;
import com.bj58.ecdata.dashboard.constant.InfoSourceConst;
import com.bj58.ecdata.dashboard.databll.DatainfoBLL;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;
import com.google.common.collect.Maps;

/**
 * 信息看板Ajax Controller
 * @author zhaoxiang 2015-07-21
 */
@Path("/info/ajax")
public class InfoAjaxController extends MvcController {
//	private AbstractBaseService<InfoCore> infoService =ServiceBeanFactory.get(InfoCore.class,null);
	
	
	@POST
	@Path("/trend")
	public ActionResult getTotalTrend() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			cateEnum = EnumCate.valueOf(businessName);
			String data = DatainfoBLL.getLine_Day(cateEnum,dateGroup);
//			log.info(cateEnum+"----"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	/**
	 * 个人，免费商家，VIP 信息量趋势
	 * @return
	 */
	@POST
	@Path("/userTypeLine")
	public ActionResult getUserTypeData() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			cateEnum = EnumCate.valueOf(businessName);
			String data = DatainfoBLL.getUserTypeLineJson(cateEnum,dateGroup,indexType);
//			log.info(cateEnum+"----"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	/**
	 * 业务线  / 类别 信息量趋势
	 * @return
	 */
	@POST
	@Path("/cateTrend")
	public ActionResult getCateTrend() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			String userType = MyServletRequestUtils.getStringParameter(request, "userType", "");
			String cate2Name = ServletRequestUtils.getStringParameter(request, "cate2Name", "");
			cateEnum = EnumCate.valueOf(businessName);
			String data =null;
			if(EnumCate.total.equals(cateEnum)){//业务线
				data =DatainfoBLL.getBusinessLineJson(cateEnum,dateGroup,indexType,userType);
			}else{//类别
				data =DatainfoBLL.getCateLineJson(cateEnum,dateGroup,cate2Name,userType);
			}
//			log.info(cateEnum+"----"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	/**
	 * 分平台来源 饼图
	 * @return
	 */
	@POST
	@Path("/platSrc")
	public ActionResult platSrc() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate", "2015-01-01");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			String userType = MyServletRequestUtils.getStringParameter(request, "userType", "");
			
			cateEnum = EnumCate.valueOf(businessName);
			String data = DatainfoBLL.getSourcePieDataJson(cateEnum,beginDate,userType, indexType);
//			log.info(cateEnum+"----"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	/**
	 * 分平台,来源,类别 曲线图
	 * @return
	 */
	@POST
	@Path("/platSrcLine")
	public ActionResult platSrcLine() {
		try {
			HttpServletRequest request = beat.getRequest();
			Map<String, String> paramJSON = Maps.newHashMap();
			paramJSON.put("businessName", MyServletRequestUtils.getStringParameter(request, "businessName", ""));
			paramJSON.put("userType", MyServletRequestUtils.getStringParameter(request, "userType", ""));
			paramJSON.put("platform", MyServletRequestUtils.getStringParameter(request, "platform", ""));
			paramJSON.put("indexType", MyServletRequestUtils.getStringParameter(request, "indexType", ""));
			paramJSON.put("dateGroup", MyServletRequestUtils.getStringParameter(request, "dateGroup", ""));
			paramJSON.put("statType", MyServletRequestUtils.getStringParameter(request, "statType", ""));
			
			String data = DatainfoBLL.getPlatCateSrcJson(paramJSON);
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	
	/**
	 * 根据平台获取来源列表
	 * @return
	 */
	@POST
	@Path("/getSourceByPlat")
	public ActionResult getSourceByPlat() {
		try {
			HttpServletRequest request = beat.getRequest();
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "");
			
			List<Object> souceList =InfoSourceConst.getSources(platform);
//			log.info(cateEnum+"----"+souceList);
			return ViewResult.jsonView(souceList.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	/**
	 * 根据业务线获取类别
	 * @return
	 */
	@POST
	@Path("/getPostCateListByBusi")
	public ActionResult getCateListByBusi() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			return ViewResult.jsonView(InfoConst.getCateListByBusiness(businessName).toString());
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	/**
	 * 简历新增 曲线图
	 * @return
	 */
	@POST
	@Path("/resumeAdd")
	public ActionResult resumeAdd() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "");
			String industry = MyServletRequestUtils.getStringParameter(request, "industry", "");
			String data = DatainfoBLL.getResumeAddJson(dateGroup,industry);
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	/**
	 * 简历新增人群 饼图
	 * @return
	 */
	@POST
	@Path("/resumeCrowd")
	public ActionResult resumeCrowd() {
		try {
			HttpServletRequest request = beat.getRequest();
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			String industry = MyServletRequestUtils.getStringParameter(request, "industry", "");
			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate", "2015-01-01");
			String endDate = MyServletRequestUtils.getStringParameter(request, "endDate", "2015-01-01");
			String data = DatainfoBLL.getResumeCrowdJson(beginDate,endDate,indexType,industry);
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
		
	/**
	 * 平台，业务线发布成功率多维柱状图
	 * @return
	 */
//	@POST
//	@Path("/platPostRate")
//	public ActionResult platPostRateData() {
//		try {
//			HttpServletRequest request = beat.getRequest();
//			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
//			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "");
//			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate", "2015-01-01");
//			String endDate = MyServletRequestUtils.getStringParameter(request, "endDate", "2015-01-01");
//
////			String data = DatainfoBLL.platPostRate(indexType, beginDate, endDate);
////			log.info(resumeType+"----"+data);
//			return ViewResult.jsonView(data);
//		} catch (Exception e) {
//			e.printStackTrace();
//			return ActionResult.view("null");
//		}
//	}
	
	/**
	 * 业务线发布成功率曲线图
	 * @return
	 */
	@POST
	@Path("/catePostRate")
	public ActionResult catePostRateData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "");
			String cateName = MyServletRequestUtils.getStringParameter(request, "cateName", "");
			
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			String data = DatainfoBLL.catePostRate(cateEnum.getDBName(),platform,cateName,indexType);
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	
}
